/**
 * 
 */
package com.shiplanet.entity;

import java.util.Set;
import java.util.TreeSet;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Formula;

@Entity
@Table(name = "CATEGORY")
public class Category {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "C_ID")
	private Integer id;

	@Column(name = "NAME")
	private String name;

	@Column(name = "DESCRIPTION")
	private String description;

	@Column(name = "IS_POPULAR")
	private boolean isPopular;

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "PARENT_ID")
	private Category parent;

	@OneToMany(cascade = CascadeType.ALL, mappedBy = "parent", fetch = FetchType.EAGER)
	private Set<Category> children = new TreeSet<Category>();

	@OneToMany(mappedBy = "category")
	private Set<Post> posts = new TreeSet<Post>();

	@OneToMany(cascade = CascadeType.ALL, mappedBy = "category", fetch = FetchType.EAGER, orphanRemoval = true)
	private Set<Photo> photos = new TreeSet<Photo>();

	@Formula("(select count(*) from POST p where p.C_ID = C_ID)")
	private Integer countPost;

	public Category(String name) {
		this.name = name;
	}

	public Category() {
	}

	public boolean addChildCategory(Category child) {
		if (child != null) {
			// prevent duplicated categories
			boolean isAdded = this.getChildren().add(child);
			if (isAdded) {
				child.setParent(this);
				return true;
			}
		}
		return false;
	}

	public boolean removeChildCategory(Category child) {
		if (child != null) {
			boolean isRemoved = getChildren().remove(child);
			if (isRemoved) {
				child.setParent(null);
				return true;
			}
		}
		return false;
	}

	public boolean addPhoto(Photo photo) {
		boolean isAdded = getPhotos().add(photo);
		if (isAdded) {
			photo.setCategory(this);
			return true;
		}

		return false;
	}

	public boolean removePhoto(Photo photo) {
		boolean isRemoved = getPhotos().remove(photo);
		if (isRemoved) {
			photo.setCategory(this);
			return true;
		}

		return false;
	}

	public Integer getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String getDescription() {
		return description;
	}

	public Category getParent() {
		return parent;
	}

	public Set<Post> getPosts() {
		return posts;
	}

	public Set<Category> getChildren() {
		return children;
	}

	public Set<Photo> getPhotos() {
		return photos;
	}

	public Integer getCountPost() {
		return countPost;
	}

	public boolean isPopular() {
		return isPopular;
	}

	public void setPopular(boolean isPopular) {
		this.isPopular = isPopular;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setPhotos(Set<Photo> photos) {
		this.photos = photos;
	}

	@SuppressWarnings("unused")
	private void setId(Integer id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setPosts(Set<Post> posts) {
		this.posts = posts;
	}

	public void setChildren(Set<Category> child) {
		this.children = child;
	}

	public void setParent(Category parent) {
		this.parent = parent;
	}

	public boolean equals(Object o) {
		if (o == null || !(o instanceof Category))
			return false;
		Category cat = (Category) o;

		if (this == o)
			return true;

		if (this.name != null && cat.name != null && this.name.equals(cat.name))
			return true;

		return false;
	}

	public int hashCode() {
		if (name != null) {
			return name.hashCode();
		} else
			return 0;
	}

}